package cn.com.haopy.yue.ai.wx.service;


import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskUserHumanQueue;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskUserHumanSource;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskUserHumanQueueService;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskUserHumanSourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;


/**
 * @author keel
 * @date 2022/4/13
 */
@Service
@Slf4j
public class WxHumanSignService {

    @Autowired
    private AIRiskUserHumanQueueService riskUserHumanQueueService;


    @Autowired
    private AIRiskUserHumanSourceService riskUserHumanSourceService;


    @Transactional
    public Object useSource(Integer userId, Integer queueId, String sourceId, String sign) throws Exception {


        AIRiskUserHumanSource humanSource = new AIRiskUserHumanSource();
        humanSource.setUserId(userId);
        humanSource.setSourceId(sourceId);
        if (Objects.nonNull(queueId)) {
            humanSource.setQueueId(queueId);
        } else {
            AIRiskUserHumanQueue humanQueue = riskUserHumanQueueService.getQueueByUserId(userId);
            if (Objects.nonNull(humanQueue)) {
                humanSource.setQueueId(humanQueue.getId());

                humanQueue.setIsAllot(Boolean.TRUE);
                riskUserHumanQueueService.updateById(humanQueue);
            }


        }


        humanSource.setIsFinish(false);
        humanSource.setSign(sign);
        humanSource.setStartTime(LocalDateTime.now());
        riskUserHumanSourceService.save(humanSource);

        return ResponseUtil.ok();
    }


}
